package com.qcxy.gzy.activity;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.qcxy.gzy.activity.base.HiddenAdaptiveActivity;
import com.qcxy.gzy.socket.SocketContact;
import com.qcxy.gzy.socket.SocketPresenterImp;
import com.qxcy.gzy.util.SPUtils;
import com.squareup.picasso.Picasso;

public class FirstPageActivity extends HiddenAdaptiveActivity implements OnClickListener, SocketContact.OnSocketCallbackListener {

    private ImageView mUserInfo;

    private TextView tvUserName;

    private String mUserName;
    private String mImgUrl;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_frist_page_new);

        mUserName = SPUtils.getValueFromSharedFile(this, "userName", "");
        mImgUrl = SPUtils.getValueFromSharedFile(this, "URL", "");
        String mOpenId = SPUtils.getValueFromSharedFile(this, "OpenId", "");
        String IP = SPUtils.getValueFromSharedFile(this, "IP", "192.168.1.112");

        SocketContact.Presenter socketPresenter = new SocketPresenterImp(this);
        socketPresenter.openSocket(IP, 6789);
        socketPresenter.sendSocketCommand("strength<<>>" + mUserName + "<<>>" + "0" + "<<>>" + "0" + "<<>>" + "0" + "<<>>" + "00:00" + "<<>>" + mImgUrl + "");
        socketPresenter.sendSocketCommand("strength<<>>" + mUserName + "<<>>" + "0" + "<<>>" + "0" + "<<>>" + "0" + "<<>>" + "00:00" + "<<>>" + mImgUrl + "");
        findViewById(R.id.iv_back).setOnClickListener(this);
        findViewById(R.id.iv_sound).setOnClickListener(this);
        mUserInfo = findViewById(R.id.userinfo);
        tvUserName = findViewById(R.id.username);
        if (!TextUtils.isEmpty(mOpenId)) {
            new MyAsyncTask().execute();
        } else {
            mUserInfo.setBackground(this.getResources().getDrawable(R.drawable.default_userinfo));
            tvUserName.setText(R.string.userName);
        }

        findViewById(R.id.ll_number).setOnClickListener(this);
        findViewById(R.id.ll_time).setOnClickListener(this);
        findViewById(R.id.ll_calorie).setOnClickListener(this);

        findViewById(R.id.ll_yy).setOnClickListener(this);
        findViewById(R.id.ll_test).setOnClickListener(this);
        findViewById(R.id.ll_report).setOnClickListener(this);
        findViewById(R.id.ll_project).setOnClickListener(this);

    }

    class MyAsyncTask extends AsyncTask<String, Integer, String> {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected String doInBackground(String... params) {
            return null;
        }

        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);
            Picasso.with(FirstPageActivity.this).load(mImgUrl).into(mUserInfo);
            tvUserName.setText(mUserName);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back: {
                SPUtils.setValueSharedFile(this, "userName", "");
                SPUtils.setValueSharedFile(this, "URL", "");
                SPUtils.setValueSharedFile(this, "OpenId", "");
                FirstPageActivity.this.finish();
                break;
            }

            case R.id.iv_sound: {
                break;
            }

            case R.id.ll_number: {
                Intent intent = new Intent(FirstPageActivity.this, NumberActivity.class);
                startActivity(intent);
                break;
            }

            case R.id.ll_time: {
                Intent intent = new Intent(FirstPageActivity.this, TimeActivity.class);
                startActivity(intent);
                break;
            }

            case R.id.ll_calorie: {
                Intent intent = new Intent(FirstPageActivity.this, CalorieActivity.class);
                startActivity(intent);
                break;
            }

            case R.id.ll_yy: {
                Intent intent = new Intent(FirstPageActivity.this, LoginActivity.class);
                startActivity(intent);
                break;
            }

            case R.id.ll_report: {
                Intent intent = new Intent(FirstPageActivity.this, ReportActivity.class);
                startActivity(intent);
                break;
            }

            case R.id.ll_test: {
                Intent intent = new Intent(FirstPageActivity.this, DetectionActivity.class);
                startActivity(intent);
                break;
            }

            case R.id.ll_project: {
                Intent intent = new Intent(FirstPageActivity.this, ProgramActivity.class);
                startActivity(intent);
                break;
            }

            default:
                break;
        }
    }

    @Override
    public void onSocketConnectSuccess() {
    }

    @Override
    public void onSocketCloseSuccess() {
    }

    @Override
    public void onSocketSendDataSuccess() {
    }

    @Override
    public void onSocketReceiveDataSuccess(String receiveMsg) {
    }

    @Override
    public void onSocketError(String errorMsg) {
    }
}
